package com.tiantianyuegou.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 
 *@Title:时间转换工具类
 *@Description:
 *@Author:admin
 *@Since:2015-7-28
 *@Version:1.1.0
 */
public class TimeUtils {

	private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	private static final SimpleDateFormat FORMAT_YMD = new SimpleDateFormat("yyyy-MM-dd");

	private static final SimpleDateFormat FORMAT_YMD_1 = new SimpleDateFormat("yyyy.MM.dd");
	//private static final SimpleDateFormat FORMAT_YMD_2 = new SimpleDateFormat("yyyy-MM-dd");


	/**
	 * 转换时间格式
	 * @param time
	 * @return
	 * @Description:
	 */
	public static String convertTime(long time) {
		if (time == 0)
			return "";
		return format.format(new Date(time));
	}

	public static String longToString(long time) {
		SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy年MM月dd日");
		if (time == 0)
			return "";
		return dataFormat.format(new Date(time));
	}

	public static String getMonthAanDay(Date d) {
		SimpleDateFormat dataFormat = new SimpleDateFormat("MM月dd日");
		if (d == null)
			return "";
		return dataFormat.format(d);
	}

	public static Date getNextDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		Date nextDate = new Date(calendar.getTimeInMillis());
		return nextDate;

	}
	
	public static String converTime(String format,Date date){
		return new SimpleDateFormat(format,Locale.getDefault()).format(date);
	}

	/**
	 * 时间转换，yyy-MM-dd格式
	 * @param time
	 * @return
	 * @Description:
	 */
	public static String convertTimeYMD(long time) {
		return FORMAT_YMD.format(new Date(time));
	}
	/**
	 * 时间转换，yyy.MM.dd格式
	 * @param time
	 * @return
	 * @Description:
	 */
	public static String convertTimeYMD1(long time) {
		return FORMAT_YMD_1.format(new Date(time));
	}

	/**
	 * 时间转换，xx月xx日
	 * @param time
	 * @return
	 * @Description:
	 */
	public static String convertTimeMD(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		return sdf.format(new Date(time));
	}

	/**
	 * 时间转换 yyyy-MM-dd HH:mm:ss
	 * @param time
	 * @return
	 */
	public static String convertTimeYMDHMS(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(time));
	}
	
	/**
	 * 时间转换yyyy-MM-dd 00:00:00
	 * @param time
	 * @return
	 * @Description:
	 */
	public static String convertTimeYMD000(long time){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		return sdf.format(new Date(time));
	}

	/**
	 * 计算时间差，单位为天
	 * @param startDate
	 * @param endDate
	 * @return
	 * @Description:
	 */
	public static int daysBetween(Date startDate, Date endDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			startDate = sdf.parse(sdf.format(startDate));
			endDate = sdf.parse(sdf.format(endDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(endDate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 判断是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 * @Description:
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date2);
		if ((calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR))
				&& (calendar1.get(Calendar.DAY_OF_YEAR) == calendar2.get(Calendar.DAY_OF_YEAR))) {
			return true;
		}
		return false;
	}
}
